<template>
  <div>
    <PlusTable
      :columns="tableConfig"
      :table-data="tableData"
      :action-bar="{ buttons, width: '100px' }"
      @clickAction="handleClickButton"
      @clickActionConfirmCancel="handleClickCancel"
    />
  </div>
</template>

<script lang="ts" setup>
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import { computed } from 'vue'
import { useTable } from 'plus-pro-components'
import type { PlusColumn, ButtonsCallBackParams } from 'plus-pro-components'

interface TableRow {
  id: number
  name: string
  status: string
  tag: string
  time: Date
}

const TestServe = {
  getList: async () => {
    const data = Array.from({ length: 3 }).map((item, index) => {
      return {
        id: index,
        name: index + 'name',
        status: String(index % 3),
        tag: index === 1 ? 'success' : index === 2 ? 'warning' : index === 3 ? 'info' : 'danger',
        time: new Date()
      }
    })
    return {
      data: data as TableRow[]
    }
  }
}

const { tableData, buttons } = useTable<TableRow[]>()

buttons.value = [
  {
    // 修改
    text: '修改',
    code: 'edit',
    props: {
      type: 'primary'
    },
    show: computed(() => true),
    onClick(params: ButtonsCallBackParams) {
      if (params?.formRefs) {
        // isEdit v0.1.8 新增
        const isEdit = params.formRefs[0].isEdit.value
        isEdit ? params.formRefs[0]?.stopCellEdit() : params.formRefs[0]?.startCellEdit()
      }
    }
  },
  {
    // 删除
    text: '删除',
    code: 'delete',
    props: {
      type: 'danger'
    },
    confirm: {
      options: { draggable: true }
    },
    onClick(params: ButtonsCallBackParams) {
      console.log(params, 'onClick')
    },
    onConfirm(params: ButtonsCallBackParams) {
      console.log(params, 'onConfirm')
    },
    onCancel(params: ButtonsCallBackParams) {
      console.log(params, 'onCancel')
    }
  }
]

const tableConfig: PlusColumn[] = [
  {
    label: '名称',
    prop: 'name'
  },
  {
    label: '状态',
    prop: 'status',
    valueType: 'select',
    options: [
      {
        label: '未解决',
        value: '0',
        color: 'red'
      },
      {
        label: '已解决',
        value: '1',
        color: 'blue'
      },
      {
        label: '解决中',
        value: '2',
        color: 'yellow'
      },
      {
        label: '失败',
        value: '3',
        color: 'red'
      }
    ]
  },
  {
    label: '标签',
    prop: 'tag',
    valueType: 'tag',
    fieldProps: (value: string) => {
      return { type: value }
    }
  },
  {
    label: '时间',
    prop: 'time',
    valueType: 'date-picker'
  }
]

const getList = async () => {
  try {
    const { data } = await TestServe.getList()
    tableData.value = data || []
  } catch (error) {}
}
getList()

const handleClickButton = (params: ButtonsCallBackParams) => {
  console.log(params, 'handleClickButton')
}
const handleClickCancel = (params: ButtonsCallBackParams) => {
  console.log(params, 'handleClickCancel')
}
</script>
